package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service(interfaceClass = SetMealService.class)
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetMealDao setMealDao;
    @Transactional
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }
        //图片不为空，则操作redis进行记录
        if(!StringUtils.isEmpty(setmeal.getImg())) {
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        //从redis中获取数据
        try(Jedis jedis = jedisPool.getResource()){
            //redis中获取数据
            String setMeals = jedis.get("setMealss");
            //判断有没有
            if(null==setMeals){
                //没有数据，从数据库中查询
                List<Setmeal> setmealList = setMealDao.selectAll();
                //序列化数据成字符串
                ObjectMapper mapper = new ObjectMapper();
                try {
                    setMeals = mapper.writeValueAsString(setmealList);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                //存入redis
                jedis.set("setMealss",setMeals);
                //定时过期
                jedis.expire("setMealss",5*60);
                //返回数据
                return setmealList;
            }else{
                //如果redis中有数据，反序列化成List返回
                ObjectMapper mapper = new ObjectMapper();
                List list = new ArrayList();
                try {
                    list = mapper.readValue(setMeals, List.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return list;
            }
        }

    }

    @Override
    public Setmeal findById(Integer id) {
        //从redis中获取数据
        try(Jedis jedis = jedisPool.getResource()){
            //redis中获取数据
            String setMeal = jedis.get(String.valueOf(id));
            //判断有没有
            if(null==setMeal){
                //没有数据，从数据库中查询
                Setmeal setmeal2 = setMealDao.selectById(id);
                //序列化数据成字符串
                ObjectMapper mapper = new ObjectMapper();
                try {
                    setMeal = mapper.writeValueAsString(setmeal2);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                //存入redis
                jedis.set(String.valueOf(id),setMeal);
                //定时清理
                jedis.expire(String.valueOf(id),5*60);
                //返回数据
                return setmeal2;
            }else{
                //如果redis中有数据，反序列化成List返回
                ObjectMapper mapper = new ObjectMapper();
                Setmeal setmeal = new Setmeal();
                try {
                    setmeal = mapper.readValue(setMeal, Setmeal.class);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return setmeal;
            }
        }

    }
}
